package cn.edkuir.order.config;

import cn.edkuir.order.vo.PayVo;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCM0czsQP9itveGrr0nj3B7sRBuhShiL0aoIAMy5wBncHHsYkKrTzbxB0lWVlc3hGykKMmp3BKNvYmzp4lpFvosulywmfpgIRqvA89ogeRzjlqkxF/5QYwMiafrDG9va/WXGfYBsxI5ZHNlSe9XgPIupc9DLKqNoA9VlBXVQ6MF+1GVpA7H/BREx8sk9j08LKWJW3aBqu8Pz/eN+eQzAzl61LWOAEGFdLUB6G4XxaMuXG1u5Cow1tJ/lADJVTpb2c3R7spm9RAeHFXJpU6376j7NDbQI/yziPD4tSLrgFNRfFtPtB3zws3joO9eUPLT2gAkEOyKEtod4LgaCYNCjA0/AgMBAAECggEADdvwIShceiirv9Oexi/10BajGV19lfw1JgLQjcfyYDvO2obpLNcE/z/KVfb1an9b+eQRxOfl45FN5iKNAYzDXneQ+8iLvb5EVAk59n0iMMVH4oN5Npze3bUGk7mLlhm3+AEUFyK6nm9mdAeMyL3mF/U1qtTQjFO+bpu+cljPyJzGwu/ipcsenJ28FQcZd8s0FiKqXBpoGtDbAi6H/L1o3TUVVYu16KASe8RDNE698JnZ2L0lnLnKV95W4m6pLcxHARhwj5qVSRPk3xDWWhavRntGEyX8wJW++3haIJG3MTCnSZsS6Oh6hRVjxdYXnpKTpFzdR/xfRWYGBg8w8sOOAQKBgQD3IMiVpKJl6xQCstOlXHoNOYXl8p+2dwQijP5mW9GedUtmiwtkpHn0FO39Ugj+tSnX6n5Pf18x+XUI+X7D1+rjurxlRyGWoi7F1gnBdm7mse8VJi446wFcIgeA/IBc3jn3j3kE8cabuncwFXZVL2AflxfLC9A7fYcxJEan/HKAgQKBgQCR3/6TWJ+ZPvABLBNA1orUll+B184CzuUpQ5mwWuir017RpVOBVy1GSkyjti45p3v+Hcy3dCFgy6FwR6PVg8vL8398iiZP0rkDzt3HFkd40EX7i3nHA+LOVzh738PhsjazrRYBwAt1pqqL9OAdVpi/VSYnQRJ3LwX2kJVApMetvwKBgC6oBNajt3lQcX2071pX5D8c7hekv9x+CczQG7ONgDzPXkXk2GjO2KtpTCk4VLRp1ZWLEv/FR16y/xKLsBAfcpw+BBWG4WOECRqWt07MIlHxo/a1N6LNU3tmfU4v3bgqyHzhnHJ/hwSdpqTwWF/qytYL+5T9DPo+T8jKVYCfm6qBAoGAOmrOYyl5eIA40u9Yon2ISUwS4UeP65X5NYYp1PMC/FSAQ0qG32QO8Hguqtz5NAN7Zy993PW3rDC/7NvzozzNb1aK9k/Iuey0RO8h52lno90cOXkIgodu250YNJtOsWKR/3BQQNEshagB4lxlJ7qXHwA1cEDh6TK+m5gY/TM/97cCgYANYoqV9VZfIajyCxyMKqv9vUcZXySjkC2HDKB6I8qsTa3emvCpYzjamSkmB35QZn+CZDXniAcVmCELaaWAoGSkAXE/9+LxYjQdjqNHjdE7aT4eQLIejcEpN493qglntpwgdeXCBfeIdUZA7hbJawY5u6dOjnL9bEB5pUzsdn2UoA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv3PvpeZzV0mkMQajcatABguRiNiXJ+4wvgxKBhks0mGOhQ8RkoyPahe0WBZBhze+teK/SeRa8pcVotOj/jWlkKLBFMr2dkR5Wgh1RlJb9OFW4Vql8xBK8egAL9uOfLKfeSLZz0quhVN5EjN3mZCYvDQJP7pwXfdTudPx3zGMcWgnefgjS8p7eNH+6isYyMc0l2RlOEagGF2OLX+yE/WD2sVeRWTWG+v4yfp0wE38QXJzIadDmEXwOUdjfqmRCqo888E8PUPLSc9+rHaFbMpbpV6uYboTZAx4atxwap7ZdzOp4cWsRTE51AhEEtqWdFYEGohoDYw57Tx41HCFXPDkzQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url;

    // 签名方式
    private String sign_type;

    // 字符编码格式
    private String charset;

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl;


    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
